import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/models/report/studentStats.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';

import '../../themes/pageStyles.dart';

// ignore: must_be_immutable
class StudentsStatisticsDataGrid extends StatelessWidget {
  StudentsStatisticsDataGrid({Key? key, required this.items});

  final GlobalKey<SfDataGridState> studentGridkey =
      GlobalKey<SfDataGridState>();
  List<StudentStats> items;
  DataGridController _dataGridController = DataGridController();
  final PageStyles _pageStyles = PageStyles();

  @override
  Widget build(BuildContext context) {
    Log.debug('build StudentsStatisticsDataGrid');

    return this.items.isNotEmpty
        ? Column(children: [Expanded(child: _buildDataGrid())])
        : Center(child: Text('Data not available ...'));
  }

  Container _buildDataGrid() {
    return Container(
      decoration: _pageStyles.dataGridBackground(),
      child: Container(
        decoration: _pageStyles.dataGridForground(),
        margin: EdgeInsets.all(12.0),
        child: SfDataGrid(
          source: StudentDataGridSource(items: items),
          onQueryRowHeight: (row) {
            return row.rowIndex == 0 ? 50.0 : 49.0;
          },
          //设置表头高度
          key: studentGridkey,
          showCheckboxColumn: false,
          rowsPerPage: 15,
          allowSorting: true,
          allowTriStateSorting: true,
          allowSwiping: true,
          navigationMode: GridNavigationMode.row,
          controller: _dataGridController,
          columns: <GridColumn>[
            GridColumn(
              columnName: 'TimeSlot',
              width: 140,
              label: buildDataGridColumnLabel('Time Slot'),
            ),
            GridColumn(
              width: 100,
              columnName: 'Read',
              label: buildDataGridColumnLabel('Read'),
            ),
            GridColumn(
              width: 100,
              columnName: 'Quiz',
              label: buildDataGridColumnLabel('Quiz'),
            ),
            GridColumn(
              width: 100,
              columnName: 'Point',
              label: buildDataGridColumnLabel('Points'),
            ),
            GridColumn(
              columnName: 'TimeLogin',
              width: 200,
              label: buildDataGridColumnLabel('Time Login'),
            ),
            GridColumn(
              columnName: 'Incentives',
              width: 200,
              label: buildDataGridColumnLabel('Incentives'),
            ),
          ],
        ),
      ),
    );
  }
}

class StudentDataGridSource extends DataGridSource {
  StudentDataGridSource({required List<StudentStats> items}) {
    dataGridRows = items
        .map<DataGridRow>((dataGridRow) => DataGridRow(cells: [
              DataGridCell<String>(
                  columnName: 'TimeSlot', value: dataGridRow.daysInterval),
              DataGridCell<int>(
                  columnName: 'Read', value: dataGridRow.noOfBookRead),
              DataGridCell<int>(
                  columnName: 'Quiz', value: dataGridRow.noOfQuiz),
              DataGridCell<int>(columnName: 'Point', value: dataGridRow.points),
              DataGridCell<int>(
                  columnName: 'TimeLogin', value: dataGridRow.logInTime),
              DataGridCell<int>(
                  columnName: 'Incentives', value: dataGridRow.incentiveTime),
            ]))
        .toList();
  }

  List<DataGridRow> dataGridRows = [];

  @override
  List<DataGridRow> get rows => dataGridRows;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    //列表隔行Row的颜色 // https://help.syncfusion.com/flutter/datagrid/conditional-styling
    Color setRowBackgroundColor() {
      final int index = effectiveRows.indexOf(row);
      if (index % 2 != 0) {
        return Get.isDarkMode ? Colors.black : Colors.grey[200]!;
      }
      return Colors.transparent;
    }

    return DataGridRowAdapter(
        color: setRowBackgroundColor(),
        cells: row.getCells().map<Widget>((dataGridCell) {
          if (dataGridCell.columnName == 'TimeLogin' ||
              dataGridCell.columnName == 'Incentives') {
            return Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.symmetric(horizontal: 16.0),
                child: Text(
                  getTimeDiff(dataGridCell.value),
                  style: Get.textTheme.bodySmall,
                ));
          } else {
            return Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.symmetric(horizontal: 16.0),
                child: Text(
                  dataGridCell.value.toString(),
                  overflow: TextOverflow.ellipsis,
                  style: Get.textTheme.bodySmall,
                ));
          }
        }).toList());
  }
}
